﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using WMSADBUtility;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.SY
{
    public partial class SystemMaintenanceFrm : Form
    {
        DataBaseControl backdata = new DataBaseControl();
        public SystemMaintenanceFrm()
        {
            InitializeComponent();
        }
        private string path = string.Empty;
        private string fileName = string.Empty;
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (this.rbBackupData.Checked)
            {

                fileName = string.Empty;
                SaveFileDialog sfile = new SaveFileDialog();
                sfile.Filter = "保存(*.bak)|*.bak";
                sfile.FilterIndex = 1;
                sfile.RestoreDirectory = true;
                if (DialogResult.OK == sfile.ShowDialog())
                {
                    fileName = DateTime.Now.ToString("yyyyMMdd") + "-" + sfile.FileName.Substring(sfile.FileName.LastIndexOf("\\") + 1);
                    path = sfile.FileName.Substring(0, sfile.FileName.LastIndexOf("\\")) + "\\";
                    this.txtFile.Text = path + fileName;

                    backdata.DataBaseOfBackupName = fileName;
                    backdata.DataBaseOfBackupPath = path;
                }
                else
                {
                    this.txtFile.Text = "";
                }
            }
            if (this.rbDataRestore.Checked)
            {
                fileName = string.Empty;
                OpenFileDialog sfile = new OpenFileDialog();
                sfile.Filter = "保存(*.bak)|*.bak";
                sfile.FilterIndex = 1;
                sfile.RestoreDirectory = true;
                if (DialogResult.OK == sfile.ShowDialog())
                {
                    fileName = sfile.FileName.Substring(sfile.FileName.LastIndexOf("\\") + 1);
                    path = sfile.FileName.Substring(0, sfile.FileName.LastIndexOf("\\")) + "\\";
                    this.txtFile.Text = path + fileName;

                    backdata.DataBaseOfBackupName = fileName;
                    backdata.DataBaseOfBackupPath = path;
                }
                else
                {
                    this.txtFile.Text = "";
                }
            }
        }

        /// <summary>
        /// 数据维护操作
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            #region  备份数据
            if (this.rbBackupData.Checked)
            {
                if (!SystemVariable.VaildationPermission("数据还原/备份")) return;

                if (this.txtFile.Text.Trim() == "")
                {
                    MessageClass.error("文件名不能为空！");
                    return;
                }
                if (MessageClass.enquire("确认备份数据！") == DialogResult.OK)
                {
                    backdata.BackupDataBase();
                }
            }
            #endregion

            #region 还原数据
            if (this.rbDataRestore.Checked)
            {
                if (!SystemVariable.VaildationPermission("数据还原/备份")) return;
                if (this.txtFile.Text.Trim() == "")
                {
                    MessageClass.error("文件名不能为空！");
                    return;
                }
                if (MessageClass.enquire("是否还原数据库！") == DialogResult.OK)
                {
                    backdata.ReplaceDataBase();
                }
            }
            #endregion

            #region 系统初始化
            if (this.rbInitializeSystem.Checked)
            {
                if (!SystemVariable.VaildationPermission("系统初始化")) return;

                if (this.txtFile.Text.Trim() == "")
                {
                    MessageClass.error("文件名不能为空！");
                    return;
                }
                if (MessageClass.enquire("是否系统初始化！") == DialogResult.OK)
                {
                    backdata.InitializationDataBase();
                }
            }
            #endregion
        }

        /// <summary>
        /// 备份数据选中
        /// </summary>
        private void rbBackupData_CheckedChanged(object sender, EventArgs e)
        {
            if (rbBackupData.Checked)
            {
                txtFile.Text = Application.StartupPath + "\\Databack\\" + DateTime.Now.ToString("yyyyMMdd") + "-" + "CFSDB.bak";
                backdata.DataBaseOfBackupPath = Application.StartupPath + "\\Databack\\";
                backdata.DataBaseOfBackupName = DateTime.Now.ToString("yyyyMMdd") + "-" + "CFSDB.bak";
            }
            else
            {
                this.txtFile.Text = "";
            }
        }

        /// <summary>
        /// 系统初始化
        /// </summary>
        private void rbInitializeSystem_CheckedChanged(object sender, EventArgs e)
        {
            backdata.DataBaseOfBackupPath = Application.StartupPath + "\\Databack\\";
            backdata.DataBaseOfBackupName = "EWMSA.bak";
            txtFile.Text = "****************";
        }
        /// <summary>
        /// 取消
        /// </summary>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtFile.Text = "";
            this.rbBackupData.Checked = false;
            this.rbDataRestore.Checked = false;
            this.rbInitializeSystem.Checked = false;
        }

        #region 退出
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion


    }
}
